package com.autologistics.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.autologistics.model.Admin;
import com.autologistics.model.User;

public class AdminDaoImpl implements AdminDao{
	HibernateTemplate hibTemplate;

	public HibernateTemplate getHibTemplate() {
		return hibTemplate;
	}

	public void setHibTemplate(HibernateTemplate hibTemplate) {
		this.hibTemplate = hibTemplate;
	}
	/**
	 * @param ac
	 * @param pwd
	 * @return ����Ա
	 * @author cl
	 */
	public Admin getByAccountPwd(String ac ,String pwd){
		String hql = "from Admin where account=? and pwd =?";
		List<Admin> list = hibTemplate.find(hql,new Object[]{ac,pwd});
		if (!list.isEmpty()){
			return list.get(0);
		}else{
			return null;
		}
	}
	public List<Admin> getByRole(int role){
		String hql ="from Admin where role=?";
		List<Admin> list = hibTemplate.find(hql,role);
		if (!list.isEmpty()){
			return list;
		}else{
			return null;
		}
	}
	public Admin getById(int id){
		String hql = "from Admin where id=?";
		List<Admin> list = hibTemplate.find(hql,id);
		if (!list.isEmpty()){
			return list.get(0);
		}else{
			return null;
		}
	}
	public Admin getByAccoint(String ac){
		String hql = "from Admin where account=? ";
		List<Admin> list = hibTemplate.find(hql,ac);
		if (!list.isEmpty()){
			return list.get(0);
		}else{
			return null;
		}
	}
	public List<Admin> getExceptAll(){
		String hql="from Admin where role<>0";
		List<Admin> list = hibTemplate.find(hql);
		if (!list.isEmpty()){
			return list;
		}else{
			return null;
		}
	}
	public void save(Admin admin){
		hibTemplate.save(admin);
	}
	public void update(Admin admin){
		hibTemplate.update(admin);
	}
	public List<Admin> queryForOnePage(final int offset, final int length){
		String hql = "from Admin where role<>0";
		Session session = hibTemplate.getSessionFactory().openSession();
		Query q = session.createQuery(hql);  
	    q.setFirstResult(offset);  
	    q.setMaxResults(length); 
	    List<Admin> list=q.list();
	    session.close();
		if (!list.isEmpty()){
			return list;
		}else{
			return null;
		}
	}
}
